//<my-matriculator - a simple tool for registering racers in a USAC cyclocross race>
//Copyright (C) <2011>  <Steven Rivers>

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using Registration.Model.Entities;

namespace Registration.Model
{
    class UsacDb
    {
        public static IList<UsacRider> FindRider(string s)
        {

            var racers = GetAllRacers();
            var matches = from rider in racers
                          where rider.LastName.ToLower().Contains(s) || rider.FirstName.ToLower().Contains(s) ||  rider.License.ToString().ToLower().Contains(s)
                          select rider;
            return matches.ToList();

        }

        public static UsacRider FindRider(string lname, string fname)
        {

            var racers = GetAllRacers();
            var matches = from rider in racers
                          where rider.LastName.ToUpper() == lname.ToUpper() && rider.FirstName.ToUpper() == fname.ToUpper()
                          select rider;
            return matches.FirstOrDefault();

        }

        public static IList<UsacRider> FindByLicense(string s)
        {

            var racers = GetAllRacers();

            var matches = from rider in racers
                          where rider.License.ToString().ToLower().StartsWith(s)
                          select rider;
            return matches.ToList();
        }

        public static UsacRider FindFirstRacer(string search)
        {
      
            var racers = GetAllRacers();
            var matches = from rider in racers
                          where rider.License.ToString().StartsWith(search.ToLower())
                          select rider;

            return matches.FirstOrDefault();
        }


        public static ICollection<UsacRider> GetAllRacers()
        {
            var ds = UsacDbDataset.ReadDataSet();
            var racers = new ObservableCollection<UsacRider>();
            foreach (var r in from DataRow racerRow in ds.Tables["Racer"].Rows 
                              select new UsacRider( (int)racerRow["License"], (string)racerRow["LastName"], (string)racerRow["FirstName"], (string)racerRow["City"],
                                                    (string)racerRow["State"], (string)racerRow["Gender"], (string)racerRow["RacingAge"], (string)racerRow["ExpDate"], 
                                                    (string)racerRow["Club"], (string)racerRow["Team"], 
                                                    (string)racerRow["NCAA"], (string)racerRow["CrossCategory"]))
            {
                racers.Add( r );
            }
            return racers;
        }

        public static UsacRider GetRacer(int id)
        {
            UsacRider r = null;
            var ds = UsacDbDataset.ReadDataSet();
            var table = ds.Tables["Racer"];

            var expression = "License = " + id.ToString();
            var rows = table.Select(expression);

            if ( rows.Length > 0 )
            {

                var racerRow = rows[0];

                r = new UsacRider((int)racerRow["License"], (string)racerRow["LastName"], (string)racerRow["FirstName"], (string)racerRow["City"],
                                  (string)racerRow["State"], (string)racerRow["Gender"], (string)racerRow["RacingAge"], (string)racerRow["ExpDate"],
                                  (string)racerRow["Club"], (string)racerRow["Team"],
                                  (string)racerRow["NCAA"], (string)racerRow["CrossCategory"]);
            }
            return r;
        }
    }
}